package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.GlowedNodeTypeServiceAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author zhoubang
 * 云聚节点-云联网专线接入产品-新版-路由协议类型属性联动
 */
@Service
@AttrHandler(GlowedNodeTypeServiceAttrHandler.class)
public class GlowedNodeTypeServiceAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    /*@Override
    public void handle(KiteAttr kiteAttr, KiteInst inst, AttrData data) {
        if(KiteStringUtils.equals(kiteAttr.getValue(), "110008383")){
            //正式节点
            inst.getAttr("200002146").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            inst.getAttr("200002147").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
        }else if(KiteStringUtils.equals(kiteAttr.getValue(), "110008384")){
            //测试节点
            inst.getAttr("200002147").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            inst.getAttr("200002146").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
        }
    }

    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData data) {

    }*/

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String sceneInstId = data.getSceneInstId();

        // 主框架节点接入带宽与子框架的联动处理
        setNodeBandWidthMode(attrValue, inst, sceneInstId);
    }
    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void setNodeBandWidthMode(String value, KiteInst inst, String sceneInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        kiteProdInstList.stream().forEach(prodInst ->{
            // 节点接入带宽
            if ( "10003509".equals(prodInst.getSpecId())){
                if(KiteStringUtils.equals(value, "110008383")){
                    //正式节点
                    prodInst.getAttr("200002146").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    prodInst.getAttr("200002147").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                }else if(KiteStringUtils.equals(value, "110008384")){
                    //测试节点
                    prodInst.getAttr("200002147").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    prodInst.getAttr("200002146").setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                }
            }
        });
    }

}
